<template>
  <div
    :style="`width:${realWidth};height:${realHeight};`"
    class="tdesign-image-viewer__base"
  >
    <t-image-viewer :images="realSrcList" v-model:visible="visible">
      <template #trigger>
        <div class="tdesign-image-viewer__ui-image">
          <img
            :src="`${realSrc}`"
            class="tdesign-image-viewer__ui-image--img"
            fit="cover"
          />
          <div class="tdesign-image-viewer__ui-image--hover" @click="onOpen">
            <span><t-icon name="browse" size="1.4em" /> 预览</span>
          </div>
        </div>
      </template>
    </t-image-viewer>
  </div>
</template>

<script setup>
import { isExternal } from "@/utils/validate";

const props = defineProps({
  src: {
    type: String,
    default: "",
  },
  width: {
    type: [Number, String],
    default: "100",
  },
  height: {
    type: [Number, String],
    default: "100",
  },
});

const realSrc = computed(() => {
  if (!props.src) {
    return;
  }
  let real_src = props.src.split(",")[0];
  if (isExternal(real_src)) {
    return real_src;
  }
  return import.meta.env.VITE_APP_BASE_API + real_src;
});

const realSrcList = computed(() => {
  if (!props.src) {
    return;
  }
  let real_src_list = props.src.split(",");
  let srcList = [];
  real_src_list.forEach((item) => {
    if (isExternal(item)) {
      return srcList.push(item);
    }
    return srcList.push(import.meta.env.VITE_APP_BASE_API + item);
  });
  return srcList;
});

const realWidth = computed(() =>
  typeof props.width == "string" ? props.width : `${props.width}px`
);

const realHeight = computed(() =>
  typeof props.height == "string" ? props.height : `${props.height}px`
);

const visible = ref(false);
const onOpen = () => {
  visible.value = true;
};
</script>

<style lang="scss" scoped>
.tdesign-image-viewer__ui-image {
  width: 100%;
  height: 100%;
  display: inline-flex;
  position: relative;
  justify-content: center;
  align-items: center;
  border-radius: var(--td-radius-small);
  overflow: hidden;
}

.tdesign-image-viewer__ui-image--hover {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  background-color: rgba(0, 0, 0, 0.6);
  color: var(--td-text-color-anti);
  line-height: 22px;
  transition: 0.2s;
}

.tdesign-image-viewer__ui-image:hover .tdesign-image-viewer__ui-image--hover {
  opacity: 1;
  cursor: pointer;
}

.tdesign-image-viewer__ui-image--img {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  cursor: pointer;
  position: absolute;
}

.tdesign-image-viewer__ui-image--footer {
  padding: 0 16px;
  height: 56px;
  width: 100%;
  line-height: 56px;
  font-size: 16px;
  position: absolute;
  bottom: 0;
  color: var(--td-text-color-anti);
  background-image: linear-gradient(
    0deg,
    rgba(0, 0, 0, 0.4) 0%,
    rgba(0, 0, 0, 0) 100%
  );
  display: flex;
  box-sizing: border-box;
}

.tdesign-image-viewer__ui-image--title {
  flex: 1;
}

.tdesign-popup__reference {
  margin-left: 16px;
}

.tdesign-image-viewer__ui-image--icons .tdesign-icon {
  cursor: pointer;
}

.tdesign-image-viewer__base {
  // width: 160px;
  // height: 160px;
  margin: 10px;
  border: 4px solid var(--td-bg-color-secondarycontainer);
  border-radius: var(--td-radius-medium);
}
</style>
